package com.abigdreamer.im.server.module.msgProcess;

import java.util.List;
import java.util.Map;

import com.abigdreamer.im.message.BaseMessage;
import com.abigdreamer.im.message.MessageType;
import com.abigdreamer.im.message.OnlineMessage;
import com.abigdreamer.im.message.SysMessage;
import com.abigdreamer.im.server.log.EIMLogger;
import com.abigdreamer.im.server.module.ServerCache;
import com.abigdreamer.im.server.module.UserConnect;

public class OnlineMessageProcess implements IMessageProcess {

	public void processMessage(BaseMessage message) {
		OnlineMessage onlineMessage = (OnlineMessage) message;
		String srcQQ = onlineMessage.getSrcQQ();
		SysMessage sysMsg = new SysMessage(srcQQ);
		sysMsg.setMsgContentType(MessageType.SysMessage_Online);
		ServerCache cache = ServerCache.getInstance();
		Map<String, UserConnect> userConnectMap = cache.getUserConnectMap();
		for (String o : userConnectMap.keySet()) {
			UserConnect userConnect = userConnectMap.get(o);
			String destQQ = userConnect.getOwerQQ();
			if (!srcQQ.equals(destQQ)) {
				userConnect.sendTextMessage(sysMsg);
				EIMLogger.INFO("转发了" + sysMsg);
			} else {
				// 转发留言
				Map<String, List<BaseMessage>> allLeavemsg = cache.getAllLeaveMessageMap();
				List<BaseMessage> leaveMessages = allLeavemsg.get(srcQQ);// 留言信息
				for (BaseMessage msg : leaveMessages) {
					userConnect.sendTextMessage(msg);
				}
			}
		}
	}

}
